package com.shishuo.cms.tag;

import static freemarker.template.ObjectWrapper.BEANS_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.vo.GoodsItemEntityVo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
@Service
public class GoodsItemListTag extends BaseTagPlugin {
	

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String aliasName = params.get("aliasName").toString();
		int page = Integer.parseInt(params.get("p").toString());
		int orderNum = Integer.parseInt(params.get("orderNum").toString());
		int pageSize=Integer.parseInt((params.get("pageSize")!=null)?params.get("pageSize").toString():"12");
		try {
			ClassesEntity classesEntity=classesService.getByAliasName(aliasName);
			if (null!=classesEntity) {
				PageVo<GoodsItemEntityVo>  pageList=goodsItemService.getAllListPageByClassId(page,classesEntity.getId(),aliasName,orderNum,pageSize);
				env.setVariable("tag_goods_item_list", BEANS_WRAPPER.wrap(pageList));
			}
			env.setVariable("tag_class", BEANS_WRAPPER.wrap(classesEntity));
			body.render(env.getOut());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
